package flc.ast.fragment;

import android.app.Dialog;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.AlbumActivity;
import flc.ast.activity.PlayLinkActivity;
import flc.ast.activity.SelPicActivity;
import flc.ast.activity.SelVideoActivity;
import flc.ast.activity.VideoTakeActivity;
import flc.ast.adapter.AlbumAdapter;
import flc.ast.bean.AlbumBean;
import flc.ast.bean.RecordBean;
import flc.ast.databinding.FragmentPlayBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.TimeUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link PlayFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class PlayFragment extends BaseNoModelFragment<FragmentPlayBinding> {

    private Dialog myAddAlbumDialog, myEditDialog, myRenameDialog, myDeleteDialog;
    private EditText dialogRename,dialogAdd;
    private List<AlbumBean> listShow = new ArrayList<>();
    private AlbumAdapter albumAdapter;
    private int selPosition = 0;

    @Override
    protected int onCreate() {
        return R.layout.fragment_play;
    }

    @Override
    protected void initView() {
        mDataBinding.ivTakeVideo.setOnClickListener(this);
        mDataBinding.ivPlayLocal.setOnClickListener(this);
        mDataBinding.ivPlayLink.setOnClickListener(this);
        mDataBinding.ivPlayFormat.setOnClickListener(this);
        mDataBinding.tvAddAlbum.setOnClickListener(this);
        mDataBinding.tvAdd.setOnClickListener(this);
        albumAdapter = new AlbumAdapter();
        mDataBinding.rvAlbumList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mDataBinding.rvAlbumList.setAdapter(albumAdapter);
        albumAdapter.addChildClickViewIds(R.id.ivAlbumItemEdit);
        albumAdapter.setOnItemChildClickListener(this);
        albumAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivTakeVideo:
                startActivity(VideoTakeActivity.class);
                break;
            case R.id.ivPlayLocal:
                SelVideoActivity.type = 8;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.ivPlayLink:
                startActivity(PlayLinkActivity.class);
                break;
            case R.id.ivPlayFormat:
                SelVideoActivity.type = 9;
                startActivity(SelVideoActivity.class);
                break;
            case R.id.tvAddAlbum:
                addAlbumDialog();
                myAddAlbumDialog.show();
                break;
            case R.id.tvAdd:
                addAlbumDialog();
                myAddAlbumDialog.show();
                break;
            case R.id.tvDialogAddAlbumCancel:
                myAddAlbumDialog.dismiss();
                break;
            case R.id.tvDialogAddAlbumConfirm:
                if (dialogAdd.getText().toString().trim().isEmpty()){
                    ToastUtils.showShort(getString(R.string.name_not_null));
                    return;
                }
                addAlbum(dialogAdd.getText().toString());
                getData();
                myAddAlbumDialog.dismiss();
                break;
            case R.id.tvDialogAlbumEditRename://重命名
                renameDialog();
                myEditDialog.dismiss();
                myRenameDialog.show();
                dialogRename.setText(listShow.get(selPosition).getAlbumName());
                break;
            case R.id.tvDialogRenameConfirm:
                if (dialogRename.getText().toString().trim().isEmpty()){
                    ToastUtils.showShort(getString(R.string.name_not_null));
                    return;
                }
                String path = PathUtils.getFilesPathExternalFirst() + "/"+listShow.get(selPosition).getAlbumName();
                FileUtils.rename(path,dialogRename.getText().toString().trim());
                listShow.get(selPosition).setAlbumName(dialogRename.getText().toString().trim());
                SPUtil.putObject(mContext, listShow, new TypeToken<List<AlbumBean>>() {
                }.getType());
                getData();
                myRenameDialog.dismiss();
                break;
            case R.id.tvDialogRenameCancel:
                myRenameDialog.dismiss();
                break;
            case R.id.tvDialogAlbumEditDelete://删除
                deleteDialog();
                myEditDialog.dismiss();
                myDeleteDialog.show();
                break;
            case R.id.tvDialogDeleteAlbumConfirm:
                String path1 = PathUtils.getFilesPathExternalFirst() + "/"+listShow.get(selPosition).getAlbumName();
                FileUtils.delete(path1);
                listShow.remove(selPosition);
                SPUtil.putObject(mContext, listShow, new TypeToken<List<AlbumBean>>() {
                }.getType());
                getData();
                myDeleteDialog.dismiss();
                break;
            case R.id.tvDialogDeleteAlbumCancel:
                myDeleteDialog.dismiss();
                break;
            case R.id.tvDialogAlbumEditCancel://取消
                myEditDialog.dismiss();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        String path="/"+listShow.get(position).getAlbumName();
        AlbumActivity.albumPath=path;
        startActivity(AlbumActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        switch (view.getId()) {
            case R.id.ivAlbumItemEdit:
                editDialog();
                myEditDialog.show();
                selPosition = position;
                break;
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<AlbumBean> listSp = SPUtil.getObject(mContext, new TypeToken<List<AlbumBean>>() {
        }.getType());
        if (listSp != null && listSp.size() > 0) {
            listShow.addAll(listSp);
            for (int i = 0; i < listShow.size(); i++) {
                String path = PathUtils.getFilesPathExternalFirst() + "/" + listShow.get(i).getAlbumName();
                List<File> files = FileUtils.listFilesInDir(path);
                if (files.size() > 0) {
                    listShow.get(i).setFirstPath(files.get(i).getPath());
                    listShow.get(i).setVideoNum(files.size());
                }
            }
            albumAdapter.setList(listShow);
            mDataBinding.rvAlbumList.setVisibility(View.VISIBLE);
            mDataBinding.tvAdd.setVisibility(View.VISIBLE);
            mDataBinding.llAddAlbum.setVisibility(View.GONE);
        } else {
            mDataBinding.rvAlbumList.setVisibility(View.GONE);
            mDataBinding.tvAdd.setVisibility(View.GONE);
            mDataBinding.llAddAlbum.setVisibility(View.VISIBLE);
        }
    }

    private void addAlbum(String name) {
        List<AlbumBean> listSave = new ArrayList<>();
        listSave.add(new AlbumBean(name, 0, ""));
        List<AlbumBean> listSp = SPUtil.getObject(mContext, new TypeToken<List<AlbumBean>>() {
        }.getType());
        if (listSp != null && listSp.size() > 0) {
            listSp.addAll(listSave);
            SPUtil.putObject(mContext, listSp, new TypeToken<List<AlbumBean>>() {
            }.getType());
        } else {
            SPUtil.putObject(mContext, listSave, new TypeToken<List<AlbumBean>>() {
            }.getType());
        }
    }

    private void addAlbumDialog() {
        myAddAlbumDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_add_album, null);
        myAddAlbumDialog.setContentView(inflate);
        myAddAlbumDialog.setCancelable(true);
        Window dialogWindow = myAddAlbumDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView right = inflate.findViewById(R.id.tvDialogAddAlbumConfirm);
        TextView cancel = inflate.findViewById(R.id.tvDialogAddAlbumCancel);
        dialogAdd = inflate.findViewById(R.id.etDialogAddAlbumText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    //编辑弹框
    private void editDialog() {
        myEditDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_album_edit, null);
        myEditDialog.setContentView(inflate);
        myEditDialog.setCancelable(true);
        Window dialogWindow = myEditDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.BOTTOM);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView rename = inflate.findViewById(R.id.tvDialogAlbumEditRename);
        TextView delete = inflate.findViewById(R.id.tvDialogAlbumEditDelete);
        TextView cancel = inflate.findViewById(R.id.tvDialogAlbumEditCancel);
        rename.setOnClickListener(this);
        delete.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    //重命名弹框
    private void renameDialog() {
        myRenameDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_rename, null);
        myRenameDialog.setContentView(inflate);
        myRenameDialog.setCancelable(true);
        Window dialogWindow = myRenameDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        lp.y = 50;
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView right = inflate.findViewById(R.id.tvDialogRenameConfirm);
        TextView cancel = inflate.findViewById(R.id.tvDialogRenameCancel);
        dialogRename = inflate.findViewById(R.id.etDialogRenameText);
        right.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    private void deleteDialog() {
        myDeleteDialog = new Dialog(mContext, R.style.DialogStyle);
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.dialog_delete_album, null);
        myDeleteDialog.setContentView(inflate);
        myDeleteDialog.setCancelable(false);
        //获取当前Activity所在的窗体
        Window dialogWindow = myDeleteDialog.getWindow();
        //设置Dialog从窗体底部弹出
        dialogWindow.setGravity(Gravity.CENTER);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        //获得窗体的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8);
        // 将属性设置给窗体
        dialogWindow.setAttributes(lp);
        TextView cancel = inflate.findViewById(R.id.tvDialogDeleteAlbumCancel);
        TextView right = inflate.findViewById(R.id.tvDialogDeleteAlbumConfirm);
        cancel.setOnClickListener(this);
        right.setOnClickListener(this);
    }

}